<?php
namespace app\controller;

use think\Request;
use think\facade\Db;
use app\model\Bazaar;
use app\model\Expand;
use app\model\Campaign;
use app\model\Customer;
use app\model\Principal;

class SemArea extends Common
{

	/**
	 * 请求类型
	 *
	 * @return
	 */
	public function method(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'semarea:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$sort = $request->param('sort', 'id', 'trim');
		$order = $request->param('order', 'desc', 'trim');

		array_push( $where, ['companyId', '=', $request->sessInfo['companyId'] ] );
		array_push( $where, ['userType', '=', 1 ] );

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if($bazaarId) {
			array_push( $where, ['bazaarId', '=',  $bazaarId ] );
		}

		$userid = $request->param('userid', '', 'trim');
		if($userid) {
			array_push( $where, ['userid', '=',  $userid ] );
		}
		
		$customerid = $request->param('customerid', '', 'trim');
		if($customerid) {
			array_push( $where, ['customerid', '=',  $customerid ] );
		}

		$contact = $request->param('contact', '', 'trim');
		if($contact) {
			array_push( $where, ['contact', '=',  $contact ] );
		}
		
		$areaid = $request->param('areaid', '', 'trim');
		if($areaid) {
			array_push( $where, ['areaid', 'in',  $areaid ] );
		}

		$campaignId = $request->param('campaignId', '', 'trim');
		if($campaignId) {
			array_push( $where, ['campaignId', 'in',  $campaignId ] );
		}

		$createTimeStart = $request->param('createTimeStart', '', 'trim');
		$createTimeEnd = $request->param('createTimeEnd', '', 'trim');
		if( $createTimeStart && $createTimeEnd ) {
			array_push( $where, ['datatime', 'between', cloud_datetime_format( $createTimeStart, $createTimeEnd, true) ] );
		} else {
			array_push( $where, ['datatime', 'between', cloud_datetime_format( date('Y-m-d 00:00:00', strtotime("-1 day")), date('Y-m-d 00:00:00'), true) ] );
		}
           $roleTempId = Db::table('cfg_user')->where('id', $request->sessInfo['id'])->value('role');
		if($roleTempId) {
			$authaccount = Db::table('cfg_role')->where('companyId', $request->sessInfo['companyId'])->where('id', $roleTempId)->value('semauthaccount');
			if($authaccount) {
				array_push( $where, ['userid', 'in',  $authaccount ] );
			}
		}
		$query = Db::name('cfg_area')->field('id, bazaarId, username, campaign, area, impression, clicks, ctr, cpc, rebate, cost, cash, copynumber,
			IF(ROUND( `cost` / `copynumber`, 2 ), ROUND(`cost` / `copynumber`, 2 ), `cost`) as `copycost`')
        ->where($where)->group('userid, areaid, campaignid')->where($where)->order($sort, $order)->paginate($limit);

		$total = Db::name('cfg_area')->field(' sum(impression) as impression, sum(clicks) as clicks, sum(cpc) as cpc, sum(cost) as cost, sum(cash) as cash, sum(copynumber) as copynumber,
		  IF(ROUND( sum(`cost`) / sum(`copynumber`), 2 ), ROUND( sum(`cost`) / sum(`copynumber`), 2 ), sum(`cost`)) as `copycost`')
		  ->where($where)->find();

		return ['code'=> 0, 'msg'=> '', 'count'=> $query->total(), 'data'=> $query->items(), 'total' => $total,
			'expand' => Expand::getList($request->sessInfo, 1),
		  'customer' => Customer::getList($request->sessInfo),
		  'principal' => Principal::getList($request->sessInfo),
			'bazaar' => Bazaar::getList($request->sessInfo, 1)];
	}

	/**
	* 查询账号计划
	*
	* @param tValue
	* @return
	*/
	public function getCampaign(Request $request) {
		$userid = $request->param('userid', '', 'trim');
		if( empty($userid)) {
			return ['code'=> 0, 'campaign'=> [] ];
		}
		return ['code'=> 0, 'campaign'=> Campaign::getList($request->sessInfo, $userid)];
	}

}
